<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/Reports' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card" style="margin-top: 20px">
      <el-input
        placeholder="请输入内容"
        class="input-with-select"
        style="width: 350px"
        v-model="info.query"
        clearable
        @clear="list"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="searchName"
        ></el-button>
      </el-input>
      <el-button
        type="primary"
        style="margin-left: 10px"
        @click="$router.push('/GoodsAdd')"
        >添加商品</el-button
      >
      <!-- 表格 -->
      <el-table :data="tableData" border style="width: 100%; margin-top: 20px">
        <el-table-column label="" type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="580">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120px">
        </el-table-column>
        <el-table-column prop="goods_number" label="商品数量">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>

        <el-table-column prop="add_time" label="创建时间" width="200px">
          <template slot-scope="scope">
            <div>
              {{ scope.row.add_time | time }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="edit(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="info.pagenum"
        :page-sizes="[1, 3, 5, 7]"
        :page-size="info.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 20px"
      >
      </el-pagination>
      <!-- 编辑的模态框 -->

      <el-dialog title="编辑商品" :visible.sync="dialogVisible" width="50%">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="ruleForm.goods_name" disabled></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="goods_price">
            <el-input v-model="ruleForm.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="goods_number">
            <el-input v-model="ruleForm.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="商品重量" prop="goods_weight">
            <el-input v-model="ruleForm.goods_weight"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editAdd">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "MyVueGoods",

  data() {
    return {
      //   商品列表的数据
      tableData: [],
      //   商品列表参数
      info: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      //   总条数
      total: 0,
      // 编辑框的标识
      dialogVisible: false,
      // 编辑的表单
      ruleForm: {},
      // 正则校验
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
      },
    };
  },

  mounted() {
    this.list();
  },

  methods: {
    // 渲染
    list() {
      this.$http.get(`goods`, { params: this.info }).then((res) => {
        console.log(res);
        if (res.data.meta.status == 200) {
          this.tableData = res.data.data.goods;
          this.total = res.data.data.total;
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.info.pagesize = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.info.pagenum = val;
      this.list();
    },
    // 搜索
    searchName() {
      this.info.pagenum = 1;
      this.list();
    },
    // 删除
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete(`goods/${id}`).then((res) => {
            console.log(res);
            if (res.data.meta.status === 200) {
              this.$message.success(res.data.meta.msg);
              this.list();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //编辑
    edit(id) {
      this.dialogVisible = true;
      this.$http.get(`goods/${id}`).then((res) => {
        if (res.data.meta.status === 200) {
          this.ruleForm = res.data.data;
        }
      });
    },
    editAdd() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          return this.$message("用户信息填写不正确");
        } else {
          this.$http
        .put(`goods/${this.ruleForm.goods_id}`, this.ruleForm)
        .then((res) => {
      
          if (res.data.meta.status === 200) {
            this.$message.success("编辑商品成功");
            this.list()
            this.dialogVisible=false
          }
        });
        }})
    
    },
  },
};
</script>

<style lang="scss" scoped>
</style>